package com.gitee.wsl.ext.map


/**
 * Joins two maps together using the given [join] function.
 */
inline fun <K, V1, V2, VR> Map<K, V1>.join(other: Map<K, V2>, join: (V1?, V2?) -> VR): Map<K, VR> {
    return joinTo(LinkedHashMap(this.size + other.size), other, join)
}

/**
 * Joins two maps together using the given [join] function into the given [destination].
 */
inline fun <K, V1, V2, VR, M : MutableMap<K, VR>> Map<K, V1>.joinTo(destination: M, other: Map<K, V2>, join: (V1?, V2?) -> VR): M {
    val keys = this.keys + other.keys
    for (key in keys) {
        destination[key] = join(this[key], other[key])
    }
    return destination
}
